//
//  AreaVC.m
//  EducationLive
//
//  Created by Apple on 2017/10/19.
//  Copyright © 2017年 ZhouHao. All rights reserved.
//

#import "AreaVC.h"
#import "AreaCellNode.h"
#import "EducationLiveGloabl.h"
#import "PersonalVC.h"

@interface AreaVC ()<ASTableDelegate, ASTableDataSource>
// UI
@property (nonatomic, strong) ASTableNode *tableNode;
// Data
// 省
@property (nonatomic, strong) NSMutableArray *arrayProvince;
// 城市
@property (nonatomic, strong) NSMutableArray *arrayCity;

@end

@implementation AreaVC

#pragma mark - life cycle

- (instancetype)init
{
    if (self = [super initWithNode:[ASDisplayNode new]]) {
        [self addTableNode];
    }
    return self;
}

- (void)addTableNode
{
    _tableNode = [[ASTableNode alloc] init];
    _tableNode.backgroundColor = RGB_255(245,245,245);
    _tableNode.delegate = self;
    _tableNode.dataSource = self;
    [self.node addSubnode:_tableNode];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _tableNode.frame = self.node.bounds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 加载地区数据
    [self loadData];
    // 加载表Footer
    [self configureTableView];
    // 加载导航栏item
    [self loadNavItem];
}
// 加载表Footer
- (void)configureTableView
{
    _tableNode.view.tableFooterView = [[UIView alloc]init];
    _tableNode.view.separatorStyle = UITableViewCellSeparatorStyleNone;
}
#pragma mark - NavigationController Item Title
// 加载导航栏item
- (void)loadNavItem
{
    self.title = @"设置地区";
    
    if (_areaSecondLevel) {
        [EducationLiveGloabl setNavButton:self imageName:nil setTitle:@"完成" setTitleColor:[UIColor whiteColor] titleSize:15 action:@selector(finishAction) withFrame:CGRectMake(0, 0, 30, 30) direction:@"right"];
    }
}

- (void)finishAction
{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[PersonalVC class]]) {
            PersonalVC *personalVC =(PersonalVC *)controller;
            [self.navigationController popToViewController:personalVC animated:YES];
        }
    }
}

#pragma mark - load Data
// 加载地区数据
- (void)loadData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"plist"];
    NSArray *rootArray = [[NSArray array] initWithContentsOfFile:path];
    // 获取省份
    self.arrayProvince = [NSMutableArray array];
    [rootArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.arrayProvince addObject:obj];//获取省名 obj[@"state"]
    }];
    
    if (_areaSecondLevel) {
        // 获取城市
        NSMutableArray *citys = [NSMutableArray arrayWithArray:[self.arrayProvince objectAtIndex:_indexPathRow][@"cities"]];
        self.arrayCity = [NSMutableArray array];
        [citys enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.arrayCity addObject:obj];//获取市名 obj[@"city"]
        }];
    }
}

#pragma mark - ASTableDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_areaSecondLevel) {
        return self.arrayCity.count;
    }else{
        return self.arrayProvince.count;
    }
}

- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ASCellNode *(^cellNodeBlock)() = ^ASCellNode *() {
        AreaCellNode *cellNode = [[AreaCellNode alloc] initWithItem:_areaSecondLevel == YES ?self.arrayCity[indexPath.row][@"city"] :  self.arrayProvince[indexPath.row][@"state"] areaSecondLevel:_areaSecondLevel];
        return cellNode;
    };
    return cellNodeBlock;
}
#pragma mark - ASTableDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_areaSecondLevel) {
        
    }else{
        AreaVC *areaVC = [[AreaVC alloc]init];
        areaVC.areaSecondLevel = YES;
        areaVC.indexPathRow = indexPath.row;
        [self.navigationController pushViewController:areaVC animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
